package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.mapper.EtcMapper;
import com.usian.mapper.UserMapper;
import com.usian.pojo.Etc;
import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EtcMapper etcMapper;


    @Override
    public int findByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        int a = userMapper.selectCount(wrapper);
        return a;
    }

    @Override
    public void login(User user) {
        user.setCreatedTime(new Date());
        userMapper.insert(user);
    }

    @Override
    public User find(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name",user.getName());
        wrapper.eq("password",user.getPassword());
        return userMapper.selectOne(wrapper);
    }

    @Override
    public List<Etc> findList(Etc etc) {
        QueryWrapper<Etc> wrapper = new QueryWrapper<>();
        if ((etc.getCard()!=null && etc.getCard()!="")||(etc.getEtcCard()!=null && etc.getEtcCard()!="")){

            if (etc.getCard()!=null &&etc.getCard()!=""){
                wrapper.like("card",etc.getCard());
            }
            if (etc.getEtcCard()!=null && etc.getEtcCard()!=""){
                wrapper.like("etcCard",etc.getEtcCard());
            }


            List<Etc> etcs = etcMapper.selectList(wrapper);
            return etcs;
        }else {
            wrapper.eq("name",etc.getName());
            List<Etc> etcs = etcMapper.selectList(wrapper);
            return etcs;
        }

    }

    @Override
    public void add(Etc etc) {
        etcMapper.insert(etc);
    }

    @Override
    public Etc findById(Integer id) {
        Etc etc = etcMapper.selectById(id);
        return etc;
    }

    @Override
    public void update(Etc etc) {
        etcMapper.updateById(etc);
    }

    @Override
    public void delete(Integer id) {
        etcMapper.deleteById(id);
    }

    @Override
    public List<Etc> findAdmin(Etc etc) {
        if ((etc.getCard()!=null && etc.getCard()!="")||(etc.getEtcCard()!=null && etc.getEtcCard()!="")){
            QueryWrapper<Etc> wrapper = new QueryWrapper<>();
            if (etc.getCard()!=null &&etc.getCard()!=""){
                wrapper.like("card",etc.getCard());
            }
            if (etc.getEtcCard()!=null && etc.getEtcCard()!=""){
                wrapper.like("etcCard",etc.getEtcCard());
            }

            List<Etc> etcs = etcMapper.selectList(wrapper);
            return etcs;
        }else {
            List<Etc> etcs = etcMapper.selectList(null);
            return etcs;
        }
    }
}
